const router = require('express').Router()
const db = require('../db.config')

// 中国标准时间 转换成 年月日
function formatDateTime(datestr) {

    let date = new Date(datestr)

    var y = date.getFullYear();
    var m = date.getMonth() + 1;
    m = m < 10 ? ('0' + m) : m;
    var d = date.getDate();
    d = d < 10 ? ('0' + d) : d;
    var h = date.getHours();
    var minute = date.getMinutes();
    minute = minute < 10 ? ('0' + minute) : minute;
    // return y + '-' + m + '-' + d+' '+h+':'+minute;
    return y + '-' + m + '-' + d
}

router.get('/addstudent', (req, res) => {
    // console.log(req.query);
    let { name,  date,  address } = req.query
    
    // 转换日期格式
    let formatdate = formatDateTime(date)
    // console.log(formatdate)

    // console.log(name , age, gender ,subject , time);
    // 属性sql语句 为指定列插入数据
    sql = `insert into students (name , date, address) value (?,?,?)`
    data = [name, formatdate, address]
    // console.log(data);
    db.query(sql, data, (err, results, fields) => {
        // if (err) console.log(err);
        // console.log(results)
        res.send({
            code: 200,
            msg: '添加成功'
        })
    })
})

module.exports = router